{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import pandas as pd "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 爬取网页地址\n",
    "url = ['https://www.marketbeat.com/stocks/NYSE/SPG/institutional-ownership/','https://www.marketbeat.com/stocks/NYSE/BILL/institutional-ownership/']\n",
    "# 请求headers 模拟谷歌浏览器访问\n",
    "headers = {\n",
    "    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def reque(url):\n",
    "    return requests.get(url=url,headers=headers).text\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Company</th>\n",
       "      <th>Sector</th>\n",
       "      <th>Current Price</th>\n",
       "      <th>P/E Ratio</th>\n",
       "      <th>Market Cap</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Average Volume</th>\n",
       "      <th>Indicators</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>MSFTMicrosoft</td>\n",
       "      <td>Computer and Technology</td>\n",
       "      <td>$336.44+0.7%</td>\n",
       "      <td>37.63</td>\n",
       "      <td>$2.53 trillion</td>\n",
       "      <td>23.97 million</td>\n",
       "      <td>25.91 million</td>\n",
       "      <td>Analyst ReportInsider Selling</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>AAPLApple</td>\n",
       "      <td>Computer and Technology</td>\n",
       "      <td>$150.96-0.3%</td>\n",
       "      <td>26.86</td>\n",
       "      <td>$2.48 trillion</td>\n",
       "      <td>60.28 million</td>\n",
       "      <td>88.91 million</td>\n",
       "      <td>Analyst ReportInsider Selling</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>GOOGAlphabet</td>\n",
       "      <td>Computer and Technology</td>\n",
       "      <td>$2,973.66+1.3%</td>\n",
       "      <td>28.64</td>\n",
       "      <td>$1.98 trillion</td>\n",
       "      <td>1.23 million</td>\n",
       "      <td>1.30 million</td>\n",
       "      <td>Insider Selling</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>GOOGLAlphabet</td>\n",
       "      <td>Computer and Technology</td>\n",
       "      <td>$2,965.35+1.1%</td>\n",
       "      <td>28.56</td>\n",
       "      <td>$1.98 trillion</td>\n",
       "      <td>1.92 million</td>\n",
       "      <td>1.55 million</td>\n",
       "      <td>Analyst Report</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>AMZNAmazon.com</td>\n",
       "      <td>Retail/Wholesale</td>\n",
       "      <td>$3,477.00+2.7%</td>\n",
       "      <td>68.02</td>\n",
       "      <td>$1.76 trillion</td>\n",
       "      <td>5.35 million</td>\n",
       "      <td>3.42 million</td>\n",
       "      <td>Analyst ReportInsider Selling</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          Company                   Sector   Current Price P/E Ratio  \\\n",
       "0   MSFTMicrosoft  Computer and Technology    $336.44+0.7%     37.63   \n",
       "1       AAPLApple  Computer and Technology    $150.96-0.3%     26.86   \n",
       "2    GOOGAlphabet  Computer and Technology  $2,973.66+1.3%     28.64   \n",
       "3   GOOGLAlphabet  Computer and Technology  $2,965.35+1.1%     28.56   \n",
       "4  AMZNAmazon.com         Retail/Wholesale  $3,477.00+2.7%     68.02   \n",
       "\n",
       "       Market Cap         Volume Average Volume                     Indicators  \n",
       "0  $2.53 trillion  23.97 million  25.91 million  Analyst ReportInsider Selling  \n",
       "1  $2.48 trillion  60.28 million  88.91 million  Analyst ReportInsider Selling  \n",
       "2  $1.98 trillion   1.23 million   1.30 million                Insider Selling  \n",
       "3  $1.98 trillion   1.92 million   1.55 million                 Analyst Report  \n",
       "4  $1.76 trillion   5.35 million   3.42 million  Analyst ReportInsider Selling  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "urls = 'https://www.marketbeat.com/stocks/NASDAQ/'\n",
    "dfs = pd.read_html(reque(urls))\n",
    "dfs[0].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame(dfs[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_excel(\"hello.xlsx\",sheet_name='hello')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Company</th>\n",
       "      <th>Sector</th>\n",
       "      <th>Current Price</th>\n",
       "      <th>P/E Ratio</th>\n",
       "      <th>Market Cap</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Average Volume</th>\n",
       "      <th>Indicators</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>MSFTMicrosoft</td>\n",
       "      <td>Computer and Technology</td>\n",
       "      <td>$336.44+0.7%</td>\n",
       "      <td>37.63</td>\n",
       "      <td>$2.53 trillion</td>\n",
       "      <td>23.97 million</td>\n",
       "      <td>25.91 million</td>\n",
       "      <td>Analyst ReportInsider Selling</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>AAPLApple</td>\n",
       "      <td>Computer and Technology</td>\n",
       "      <td>$150.96-0.3%</td>\n",
       "      <td>26.86</td>\n",
       "      <td>$2.48 trillion</td>\n",
       "      <td>60.28 million</td>\n",
       "      <td>88.91 million</td>\n",
       "      <td>Analyst ReportInsider Selling</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>GOOGAlphabet</td>\n",
       "      <td>Computer and Technology</td>\n",
       "      <td>$2,973.66+1.3%</td>\n",
       "      <td>28.64</td>\n",
       "      <td>$1.98 trillion</td>\n",
       "      <td>1.23 million</td>\n",
       "      <td>1.30 million</td>\n",
       "      <td>Insider Selling</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>GOOGLAlphabet</td>\n",
       "      <td>Computer and Technology</td>\n",
       "      <td>$2,965.35+1.1%</td>\n",
       "      <td>28.56</td>\n",
       "      <td>$1.98 trillion</td>\n",
       "      <td>1.92 million</td>\n",
       "      <td>1.55 million</td>\n",
       "      <td>Analyst Report</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>AMZNAmazon.com</td>\n",
       "      <td>Retail/Wholesale</td>\n",
       "      <td>$3,477.00+2.7%</td>\n",
       "      <td>68.02</td>\n",
       "      <td>$1.76 trillion</td>\n",
       "      <td>5.35 million</td>\n",
       "      <td>3.42 million</td>\n",
       "      <td>Analyst ReportInsider Selling</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          Company                   Sector   Current Price P/E Ratio  \\\n",
       "0   MSFTMicrosoft  Computer and Technology    $336.44+0.7%     37.63   \n",
       "1       AAPLApple  Computer and Technology    $150.96-0.3%     26.86   \n",
       "2    GOOGAlphabet  Computer and Technology  $2,973.66+1.3%     28.64   \n",
       "3   GOOGLAlphabet  Computer and Technology  $2,965.35+1.1%     28.56   \n",
       "4  AMZNAmazon.com         Retail/Wholesale  $3,477.00+2.7%     68.02   \n",
       "\n",
       "       Market Cap         Volume Average Volume                     Indicators  \n",
       "0  $2.53 trillion  23.97 million  25.91 million  Analyst ReportInsider Selling  \n",
       "1  $2.48 trillion  60.28 million  88.91 million  Analyst ReportInsider Selling  \n",
       "2  $1.98 trillion   1.23 million   1.30 million                Insider Selling  \n",
       "3  $1.98 trillion   1.92 million   1.55 million                 Analyst Report  \n",
       "4  $1.76 trillion   5.35 million   3.42 million  Analyst ReportInsider Selling  "
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
